//
//  UITextView+Extension.swift
//  weibobbaj
//
//  Created by Tiffany宝 on 16/5/1.
//  Copyright © 2016年 Tiffany. All rights reserved.
//

import UIKit

extension UITextView{
    
    //将表情图片转成表情字符串发送到新浪微博
    var emotionText: String?{
        
        var result = ""
        let attr = self.attributedText
        
        //遍历富文本里的内容
        attr.enumerateAttributesInRange(NSMakeRange(0, attr.length), options: []) { (dict, range, _) -> Void in
//            print(attr.string)
            //            print(dict)
            //            print("=========")
            //            print(range)
            if let attribute = dict["NSAttachment"] as? XHTextAttachment{
                //取到的是表情字符串
                //                print("表情字符串\(attribute.emoticon?.chs)")
                result += attribute.emoticon?.chs ?? ""
            }else{
                //取到的是文字
                print(attr.string)
                result += (attr.string as NSString).substringWithRange(range)
            }
        }
        
        print(result)
        
        return result
    }
    
    func insertEmotion(emotion:EmotionModel){
        
        //图片表情上屏(图文混排) ~~ NSAttributedString
        //1.将现有文字的 attributedText 生成一个 NSMutableAttributedString
        let originAttr = NSMutableAttributedString(attributedString: self.attributedText)
        
        //2.通过表情模型生成表情图片 -> 通过图片生成 NSTextAttachment -> 通过 attachment 生成一个 NSAttributedString
        //            let image = UIImage(named: "\(emotion.folderName)/\(emotion.png)", inBundle: EmotionKeyboardViewModel.sharViewModel.loadBundle(), compatibleWithTraitCollection: nil)
        let image = UIImage(named: "\(emotion.folderName ?? "")/\(emotion.png ?? "")", inBundle: EmotionKeyboardViewModel.sharViewModel.loadBundle(), compatibleWithTraitCollection: nil)
        
        let attachment = XHTextAttachment()
        //**** 赋值 ****
        attachment.emoticon = emotion
        attachment.image = image
        //设置图片的大小
        let attachmentHW = self.font!.lineHeight
        attachment.bounds = CGRect(x: 0, y: -4, width: attachmentHW, height: attachmentHW)
        let newAttr = NSAttributedString(attachment: attachment)
        
        //3. 将第2部生成的 NSAttributedString 添加到第1步生成的 NSMutableAttributedString 里面
        //获取光标的位置
        var selectedRange = self.selectedRange
//        originAttr.appendAttributedString(newAttr)
//        originAttr.insertAttributedString(newAttr, atIndex: selectedRange.location)
        originAttr.replaceCharactersInRange(selectedRange, withAttributedString: newAttr)
        // 设置字体大小
        originAttr.addAttribute(NSFontAttributeName, value: self.font!, range: NSMakeRange(0, originAttr.length))
        
        //4. 将第3步的执行结果赋值给 textView.attributedText
        self.attributedText = originAttr
        
        //防止输入完成后光标又跳回去
        selectedRange.location += 1
        selectedRange.length = 0
        self.selectedRange = selectedRange
        
        //发送文字改变的通知
        NSNotificationCenter.defaultCenter().postNotificationName(UITextViewTextDidChangeNotification, object: nil)
        
        //文字改变的代理方法(** 隐式代理 **)
        delegate?.textViewDidChange?(self)
        
    }
}




